﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  /// <summary>
  /// Summary description for DNW_WGR_Services
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.None)]
  [ScriptService]
  public class DNW_WGR_Services : System.Web.Services.WebService {

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public String HelloWorld() {
      return "Hello World";
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetCompletionList_DnnAccounts(string prefixText, int count, string contextKey) {

      if (count == 0) {
        count = 10;
      }

      List<string> accts = new List<string>(count);
      List<WGR_Business.DropDownVal> _userList = null;
      try {
        int _portalID = -1;
        int.TryParse(contextKey, out _portalID);

        _userList = WGR_Business.DnnGuildRosterController.GetDropDown_DnnUsers(_portalID, prefixText, count);
      } catch { }

      for (int i = 0; i < count; i++) {
        if (i < _userList.Count) {
          accts.Add(_userList[i].Value);
        }
      }
      return accts.ToArray();
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public string[] GetCompletionList_DnnMains(string prefixText, int count, string contextKey) {

      if (count == 0) {
        count = 10;
      }

      List<string> mains = new List<string>(count);
      List<WGR_Business.DropDownVal> _charList = null;

      try {
        int _moduleId = -1;
        int.TryParse(contextKey, out _moduleId);

        _charList = WGR_Business.DnnGuildRosterController.GetDropDowns_MainChars(_moduleId, prefixText, count);
      } catch { }

      for (int i = 0; i < count; i++) {
        if (i < _charList.Count) {
          mains.Add(_charList[i].Value);
        }
      }

      return mains.ToArray();

    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByMain(int mainCharID) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntries_Alts(mainCharID);
      return ExtractSubsetOfCharEntries(ref _charsAll, 1, 10);
    }

    #region "Web Service - DNN - By Class"
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByClass(int dnnModule, int dnnPortal, String className, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByClass(dnnModule, dnnPortal, className, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByClass(int dnnModule, int dnnPortal, String className, Boolean mainsOnly) {
      return WGR_Business.DnnGuildRosterController.GetCharEntriesByClass_Count(dnnModule, dnnPortal, className, mainsOnly);
    }
    #endregion
    #region "Web Service - DNN - By Level"
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByLevelAll(int dnnModule, int dnnPortal, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByLevel(dnnModule, dnnPortal, 1000, 0, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByLevelAll(int dnnModule, int dnnPortal, Boolean mainsOnly) {

      return WGR_Business.DnnGuildRosterController.GetCharEntriesByLevel_Count(dnnModule, dnnPortal, 1000, 0, mainsOnly);

    }


    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByLevelRange(int dnnModule, int dnnPortal, int start, int count, int minLevel, int maxLevel, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByLevel(dnnModule, dnnPortal, maxLevel, minLevel, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByLevelRange(int dnnModule, int dnnPortal, int minLevel, int maxLevel, Boolean mainsOnly) {

      return WGR_Business.DnnGuildRosterController.GetCharEntriesByLevel_Count(dnnModule, dnnPortal, maxLevel, minLevel, mainsOnly);
    }
    #endregion
    #region "Web Service - DNN - By Profession"
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByProf(int dnnModule, int dnnPortal, String profName, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByProfession(dnnModule, dnnPortal, profName, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByProf(int dnnModule, int dnnPortal, String profName, Boolean mainsOnly) {

      return WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByProfession_Count(dnnModule, dnnPortal, profName, mainsOnly);
    }
    #endregion
    #region "Web Service - DNN - By Reputation"
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByRep(int dnnModule, int dnnPortal, String repName, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByStatEntryAndType(dnnModule, dnnPortal, repName, WGR_Business.WarcraftStatTypes.Reputation, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByRep(int dnnModule, int dnnPortal, String repName, Boolean mainsOnly) {
      List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByStatEntryAndType(dnnModule, dnnPortal, repName, WGR_Business.WarcraftStatTypes.Reputation, mainsOnly);
      return _charsAll.Count;
    }
    #endregion
    #region "Web Service - DNN - By Guild Rank"
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByGRank(int dnnModule, int dnnPortal, int guildRank, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank(dnnModule, dnnPortal, guildRank, mainsOnly);
      //List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByGuildRank(dnnModule, dnnPortal, guildRank, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByGRank(int dnnModule, int dnnPortal, int guildRank, Boolean mainsOnly) {
      return WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank_Count(dnnModule, dnnPortal, guildRank, mainsOnly);
    }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByGRankAll(int dnnModule, int dnnPortal, int start, int count, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank(dnnModule, dnnPortal, 0, 100, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);

      //List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByGuildRank(dnnModule, dnnPortal, 0, 100, mainsOnly);
      //return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
      }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByGRankAll(int dnnModule, int dnnPortal, Boolean mainsOnly) {
      return WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank_Count(dnnModule, dnnPortal, 0, 100, mainsOnly);
      }


    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<WGR_Business.GuildEntry_WS> GetCharEntries_DNN_ByGRankRange(int dnnModule, int dnnPortal, int start, int count, int minGRank, int maxGRank, Boolean mainsOnly) {
      List<WGR_Business.GuildEntry> _charsAll = WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank(dnnModule, dnnPortal, minGRank, maxGRank, mainsOnly);
      return ExtractSubsetOfCharEntries(ref _charsAll, start, count);

      //List<WGR_Business.QuickStatListItem> _charsAll = WGR_Business.DnnGuildRosterController.GetEntryStatsLIs_ByGuildRank(dnnModule, dnnPortal, minGRank, maxGRank, mainsOnly);
      //return ExtractSubsetOfCharEntries(ref _charsAll, start, count);
      }

    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public int GetCharEntriesCount_DNN_ByGRankRange(int dnnModule, int dnnPortal, int minGRank, int maxGRank, Boolean mainsOnly) {
      return WGR_Business.DnnGuildRosterController.GetCharEntriesByGuildRank_Count(dnnModule, dnnPortal, minGRank, maxGRank, mainsOnly);
      }

    #endregion

    #region "Helper Functions"
    List<WGR_Business.GuildEntry_WS> ExtractSubsetOfCharEntries(ref List<WGR_Business.GuildEntry> allCharEntries, int start, int count) {

      if (count == 0) {
        count = 10;
      }

      List<WGR_Business.GuildEntry_WS> _charsReturn = new List<WGR_Business.GuildEntry_WS>(count);

      for (int i = 0; (i < count) && (i <= allCharEntries.Count - start); i++) {
        if ((i <= allCharEntries.Count - start) && (i + start > 0)) {
          _charsReturn.Add(new WGR_Business.GuildEntry_WS(allCharEntries[i + start - 1]));
        }
      }

      return _charsReturn;
    }
    List<WGR_Business.GuildEntry_WS> ExtractSubsetOfCharEntries(ref List<WGR_Business.QuickStatListItem> allCharEntries, int start, int count) {

      if (count == 0) {
        count = 10;
      }

      List<WGR_Business.GuildEntry_WS> _charsReturn = new List<WGR_Business.GuildEntry_WS>(count);

      for (int i = 0; (i < count) && (i <= allCharEntries.Count - start); i++) {
        if ((i <= allCharEntries.Count - start) && (i + start > 0)) {
          _charsReturn.Add(new WGR_Business.GuildEntry_WS(allCharEntries[i + start - 1]));
        }
      }

      return _charsReturn;
    }
    #endregion
  }
}
